<template>
  <div class="aui-wrapper aui-page__login">
    <div class="aui-content__wrapper">
      <main class="aui-content">
        <div class="login-header">
          <h2 class="login-brand">{{ $t("brand.lg") }}</h2>
        </div>
        <div class="login-body">
          <h3 class="login-title">{{ $t("login.studentRegister") }}</h3>
          <el-form
            :model="dataForm"
            :rules="dataRule"
            ref="dataForm"
            @keyup.enter.native="dataFormSubmitHandle()"
            status-icon
          >
            <el-form-item prop="headUrl">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="dataForm.headUrl" :src="dataForm.headUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item prop="username" style="margin-top: -30px; margin-left: -20px;">
              <el-alert title="下方为学生学号请妥善保存" type="warning"></el-alert>
            </el-form-item>
            <el-form-item prop="username" style="margin-top: -30px;">
              <el-input
                v-model="dataForm.username"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item prop="realName">
              <el-input
                v-model="dataForm.realName"
                :placeholder="$t('user.realName')"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="dataForm.password"
                type="password"
                :placeholder="$t('user.password')"
              ></el-input>
            </el-form-item>
            <el-form-item prop="gender">
              <el-select
                style="width: 400px"
                v-model="dataForm.gender"
                clearable
                placeholder="请选择性别"
              >
                <el-option
                  v-for="item in genderList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="email">
              <el-input
                v-model="dataForm.email"
                :placeholder="$t('user.email')"
              ></el-input>
            </el-form-item>
            <el-form-item prop="mobile">
              <el-input
                v-model="dataForm.mobile"
                :placeholder="$t('user.mobile')"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="success"
                @click="dataFormSubmitHandle()"
                class="w-percent-100"
                >注册</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="backToLogin()"
                class="w-percent-100"
                >返回</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </main>
    </div>
  </div>
</template>
  
  <script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      uploadUrl: window.SITE_CONFIG["apiURL"] + "/pic",
      dataForm: {
        headUrl: "",
        username: "",
        realName: "",
        password: "",
        gender: 0,
        email: "",
        mobile: "",
      },
      genderList: [
        {
          label: "男",
          value: 0,
        },
        {
          label: "女",
          value: 1,
        },
        {
          label: "保密",
          value: 2,
        },
      ],
    };
  },
  computed: {
    dataRule() {
      return {
        headUrl: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        realName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        gender: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  created() {
    this.getStudentId()
  },
  methods: {
    getStudentId() {
      this.$http.get("/getStudentId").then(({ data }) => {
        this.dataForm.username = data.data;
      });
    },
    handleAvatarSuccess(res, file) {
      this.dataForm.headUrl = res.data.imgAddress;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    backToLogin() {
      this.$router.replace({ name: "login" });
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http
            .post("/register/student", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$router.replace({ name: "login" });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>